package cn.daniellee.plugin.wc;

import cn.daniellee.plugin.wc.command.WelcomeCommand;
import cn.daniellee.plugin.wc.command.WelcomeCompleter;
import cn.daniellee.plugin.wc.listener.BungeeListener;
import cn.daniellee.plugin.wc.listener.PlayerListener;
import cn.daniellee.plugin.wc.runnable.WelcomeRunnable;
import cn.daniellee.plugin.wc.storage.MysqlStorage;
import cn.daniellee.plugin.wc.storage.Storage;
import cn.daniellee.plugin.wc.storage.StorageConverter;
import cn.daniellee.plugin.wc.storage.SqliteStorage;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;

import java.util.List;

public class Welcome extends JavaPlugin {

    private static Welcome instance;

    private String prefix;

    private Storage storage;

    private BukkitTask welcomeTask;

    private boolean bungeeCord;

    private long period;

    private List<String> keywords;

    private List<String> messages;

    public void onEnable(){
        instance = this;

        if (loadConfig()) {
            getLogger().info(" ");
            getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> Welcome Loaded <<<<<<<<<<<<<<<<<<<<<<<<");
            getLogger().info(">>>>> If you encounter any bugs, please contact author's QQ: 768318841 <<<<<");
            getLogger().info(" ");

            Bukkit.getPluginManager().registerEvents(new PlayerListener(), this);

            Bukkit.getPluginCommand("welcome").setExecutor(new WelcomeCommand());
            Bukkit.getPluginCommand("welcome").setTabCompleter(new WelcomeCompleter());

            bungeeCord = Bukkit.getServer().spigot().getConfig().getBoolean("settings.bungeecord", false);

            if (bungeeCord) {
                getLogger().info("[Welcome]Running in BC model.");
                this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
                this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", new BungeeListener());
            }

            welcomeTask = new WelcomeRunnable().runTaskTimer(this, 0, 20 * 60);
        }
    }

    public boolean loadConfig() {
        getLogger().info("[Welcome]Loading config...");
        if(!getDataFolder().exists()) getDataFolder().mkdirs();
        if (storage != null) storage.close();
        storage = getConfig().getBoolean("storage.mysql.use", false) ? new MysqlStorage() : new SqliteStorage();
        if (storage.initialize()) {
            getLogger().info("[Welcome]Storage initialized.");
        } else {
            getLogger().info(" ");
            getLogger().info("[Welcome]Initializing data store failed, please edit the config and reload the plugin.".replace("&", "§"));
            getLogger().info(" ");
            Bukkit.getPluginManager().disablePlugin(this);
            return false;
        }
        // 转换存储
        if (getConfig().getBoolean("storage.mysql.use", false) && getConfig().getBoolean("storage.mysql.convert", false)) {
            SqliteStorage sqliteStorage = new SqliteStorage();
            if (sqliteStorage.initialize()) {
                StorageConverter converter = new StorageConverter(sqliteStorage, (MysqlStorage) storage);
                converter.execute();
                getConfig().set("storage.mysql.convert", false);
                saveConfig();
                getLogger().info("[Welcome]Successfully transferred Sqlite data to Mysql.");
            } else {
                getLogger().info(" ");
                getLogger().info("[Welcome]Sqlite data store initialization failed, data conversion canceled.".replace("&", "§"));
                getLogger().info(" ");
            }
        }
        prefix = "&7[&b" + getConfig().get("prompt-prefix", "Welcome") + "&7] &3: &r";
        period = Welcome.getInstance().getConfig().getLong("setting.welcome-period", 300) * 1000;
        keywords = getConfig().getStringList("setting.keywords");
        if (keywords.isEmpty()) keywords.add("Welcome");
        messages = getConfig().getStringList("setting.welcome-msg");
        if (messages.isEmpty()) keywords.add("Welcome {name} to join our server~~");
        saveDefaultConfig();
        return true;
    }

    @Override
    public void onDisable() {
        if (storage != null) storage.close();
        if (welcomeTask != null) welcomeTask.cancel();
        getLogger().info(" ");
        getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> Welcome Unloaded <<<<<<<<<<<<<<<<<<<<<<<<");
        getLogger().info(" ");
    }

    public static Welcome getInstance() {
        return instance;
    }

    public String getPrefix() {
        return prefix;
    }

    public Storage getStorage() {
        return storage;
    }

    public boolean isBungeeCord() {
        return bungeeCord;
    }

    public long getPeriod() {
        return period;
    }

    public List<String> getKeywords() {
        return keywords;
    }

    public List<String> getMessages() {
        return messages;
    }
}
